const { UI_Listen, RedDot_Key, UI_Layer } = require("../../config/ui_def");
import { Sdk_Platform } from '../../frame/constants';
const { getGachaid } = require("../tool/gacha");


cc.Class({
    extends: cc.Component,

    properties: {
        price_lab: cc.Label,
        icon_sp: cc.Sprite,
        name_lab: cc.Label,
        skill_lab: cc.Label,
        num_lab: cc.Label,
        feihua_lab: cc.Label,
    },



    // onLoad () {},

    start() {

    },

    // update (dt) {},


    initPage() {

    },

    updatePage(param) {
        if (param.info) {
            this.info = param.info
        }
        if (param.index >= 0) {
            this.index = param.index
        }
        if (this.index == 0) {
            this.price = this.info.diamond * this.info.num * this.info.discount / 10
        } else {
            this.price = this.info.diamond * this.info.num
        }
        this.price_lab.string = `${this.price}`
        this.name_lab.string = `${this.info.name}`
        this.skill_lab.string = `${this.info.description}`
        this.num_lab.string = ` x${this.info.num}`
        if (this.index == 0) {
            this.feihua_lab.string = `确认花费${this.info.diamond * this.info.num * this.info.discount / 10}花生，购买${this.info.name}吗`
        } else {
            this.feihua_lab.string = `确认花费${this.info.diamond * this.info.num}花生，购买${this.info.name}吗`
        }
        if (this.info.type == 1) {
            this.icon_sp.spriteFrame = he.load.get('item', cc.SpriteAtlas, 'plist').getSpriteFrame(`${this.info.id}`)
        } else if (this.info.type == 3) {
            this.icon_sp.spriteFrame = he.load.get('skill', cc.SpriteAtlas, 'plist').getSpriteFrame(`${this.info.icon}`)
        }
    },

    closeUI() {
    },

    closeServer() {

        this.uiBase.closePage()
    },

    buyServer() {
        let guideList = he.local.getJson('guideList', 'user')
        if (!guideList[5]) {
            return
        }
        let priStr = `20002_${this.price}`

        let canBuy = gg.data.checkItemStr(priStr, false)
        if (canBuy) {
            gg.data.checkItemStr(priStr, true)
            gg.eventManager.emit('topBar', UI_Listen.TOP_UPDATE_DIAMOND)
            gg.eventManager.emit('shop', { type: UI_Listen.SHOP_BUY, index: this.index })

            this.closeServer()
            if (guideList[6]) {
                gg.uiManager.addPage(UI_Layer.Click, {
                    name: 'getStoreItemPage',
                    cover: true,
                    info: this.info,
                    double : true,
                })
            }
            if (!guideList[6]) {
                guideList[6] = 1
                he.local.setJson('isNew', false)
                he.local.setJson('guideList', guideList, 'user')
                gg.eventManager.emit('main', { type: UI_Listen.MAIN_GUIDEEND })
            }
        } else {
            gg.uiManager.addTip('花生不足')
            gg.data.checkIcon(priStr)
        }
    },

});
